<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function get_entity(){
      $query = $this->db->get('ar_entity');
      return $query->result();
    }
    public function get_companies(){
      $sql = "SELECT * FROM gl_company";
      $query = $this->db->query($sql);
      return $query->result();
    }
    public function get_roles(){
      $sql = "SELECT * FROM app_roles";
      $query = $this->db->query($sql);
      return $query->result();
    }
    public function get_users_data($id){
      $this->db->where('user_id', $id);
      $query = $this->db->get('app_users');
      return $query->result();		
    }
    public function get_bu_id($id){
      $this->db->where('company_id', $id);
      $query = $this->db->get('gl_company');
      return $query->result();	
    }
    public function get_user_type(){
      $this->db->where('lookup_type','USER_TYPE');
      $this->db->order_by('lookup_code', 'DESC');
      $query = $this->db->get('app_lookup_values');
      return $query->result();  
    }
    public function insert_user($data){
      $this->db->query("call app_users_ins(?,?,?,?,?,?,?,?,@id,@err,@msg,?)",$data);
      $que = $this->db->query("SELECT @id as id , @err as error , @msg as msg");
      return $que->result();
    }

    public function insert_bu_access($data){
      $this->db->query("call app_bu_access_ins(?,?,?,@err,@msg)",$data);
      $que = $this->db->query("SELECT @err as error , @msg as msg");
      return $que->result();
    }

    public function update_user($data){
      $this->db->query("call app_users_upd(?,?,?,?,?,?,?,?,?,@err,@msg,?)",$data);
      $que = $this->db->query("SELECT @err as error , @msg as msg");
      echo json_encode($que->result());	
    }

    public function insert_role_access($data){
      $this->db->query("call app_user_roles_ins(?,?,?,?,?,?,@err,@msg)",$data);
      $que = $this->db->query("SELECT @err as error , @msg as msg");
      return $que->result();
    }
    public function update_role_access($data){
      $this->db->query("call app_user_roles_upd(?,?,?,?,?,@err,@msg)",$data);
      $que = $this->db->query("SELECT @err as error , @msg as msg");
      echo json_encode($que->result());
    }

    public function get_role_access($id){
      $sql = "SELECT aur.role_id role_id, ar.role_name role_name, aur.start_date start_date, aur.end_date end_date, aur.enable_flag enable_flag FROM app_user_roles aur INNER JOIN app_roles ar ON ar.role_id = aur.role_id WHERE aur.user_id = ".$id." ";
      $query = $this->db->query($sql);
      return $query->result();
    }

    public function countArr($arr,$query){
      $counter =0;
      foreach ($arr as $key ) {
          $counter++;
      }
      $arr[$counter] = $query;
      return $arr; 
    }
    public function search_users($uname,$fname,$lname){
      $sql;$arg1="";$arg2="";$arg3="";
      $arr = array();

        $setter = 0;

        if($uname != ""){
          $arg1 = "user_name LIKE '".$uname."'"; $setter = 1;
          $arr = $this->countArr($arr,$arg1);
        }
        if($fname != ""){
         $arg2 = "first_name LIKE '".$fname."'"; $setter = 1;
         $arr = $this->countArr($arr,$arg2);
        }
        if($lname != ""){
         $arg3 = "last_name LIKE '".$lname."'"; $setter = 1;
         $arr = $this->countArr($arr,$arg3);
        }
        if ($setter == 1){
        $que = implode(" AND ",$arr);
          $sql = "SELECT * FROM app_users WHERE ".$que." "; 
        }else{
          $sql = "SELECT * FROM app_users WHERE user_name LIKE '".$uname."' "; 
        }
          $query = $this->db->query($sql);
          if($query->num_rows() != 0){
            return $query->result();
          }
          else {
            $result = "No results found.";
            return $result;
          }	
    }
}